package com.qding.park.manager.service.queue.biz;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.adapterfactory.AdapterProxyCreateFail;
import com.qding.park.manager.adapter.factory.AdapterParkProxyFactory;
import com.qding.park.manager.cache.ConfigDataCacheInter;
import com.qding.park.manager.cache.UserCacheInter;
import com.qding.park.manager.pojo.TbCarlock;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.TbUserInfo;
import com.qding.park.manager.pojo.TblockcarAlarm;
import com.qding.park.manager.pojo.ap.ApCarLockAlarm;
import com.qding.park.manager.pojo.enums.CarLockStatusEnum;
import com.qding.park.manager.pojo.enums.RecordStatusEnum;
import com.qding.park.manager.pojo.sms.device.OutCheckAlarm;
import com.qding.park.manager.service.queue.mapper.TbCarlockMapper;
import com.qding.park.manager.service.queue.mapper.TbParkspaceMapper;
import com.qding.park.manager.service.queue.mapper.TblockcarAlarmMapper;
import com.qding.park.manager.sms.SmsInterfaceServiec;
import com.qding.park.target.common.adapter.LockCarAdapter;
import com.qding.park.target.common.entity.AdLockCarOrder;

@Component("lockCarBiz")
public class LockCarBiz {
    private static final Log _log = LogFactory.getLog(LockCarBiz.class);
    
    @Autowired
    private TbCarlockMapper tbCarlockMapper;
    
    @Autowired
    private TblockcarAlarmMapper tblockcarAlarmMapper;
    
    @Autowired
    private ConfigDataCacheInter configDataCacheInter;
    
    @Autowired
    private UserCacheInter userCacheInter;
    
    @Autowired
    private SmsInterfaceServiec smsInterfaceServiec;
    
    @Autowired
    private TbParkspaceMapper tbParkspaceMapper;
    
    @Autowired
    private AdapterParkProxyFactory adapterParkProxyFactory;

    
    @Transactional(rollbackFor = Throwable.class)
    public void alarmCockCar(ApCarLockAlarm apCarLockAlarm) {
        _log.info(apCarLockAlarm);
        // 报警时间
        Date alarmTime = apCarLockAlarm.getAlarmTime();
        // 卡号
        String cardCode = apCarLockAlarm.getCardCode();
        // 车牌
        String carNum = apCarLockAlarm.getCarNum();
        // 出场岗停
        String ourRoom = apCarLockAlarm.getOutRoom();
        // 停车场id
        String parkCode = apCarLockAlarm.getParkId();
        // 车场软件id
        String supplyId = apCarLockAlarm.getSupplyID();
        TbParkspace tbParkspace = configDataCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyId));
        int parkId = tbParkspace.getId();
        TbUserInfo tbUserInfo = tbCarlockMapper.queryLockUser(parkId, carNum);
        TblockcarAlarm tblockcarAlarm = new TblockcarAlarm();
        if(tbUserInfo==null){
            _log.error("没有找到锁车用户....不进行处理");
            return;
        }
        tblockcarAlarm.setCarNum(carNum);
        tblockcarAlarm.setParkID(parkId);
        tblockcarAlarm.setParkName(tbParkspace.getName());
        tblockcarAlarm.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
        tblockcarAlarm.setUserID(tbUserInfo.getId());
        int num = tblockcarAlarmMapper.save(tblockcarAlarm);
//      int userId=apCarLockAlarm.
        TbCarlock tbCarlock=tbCarlockMapper.queryByParkIdCarNum(parkId, carNum);
        if (num != 0&&tbCarlock!=null) {
            OutCheckAlarm alarm = new OutCheckAlarm();
            alarm.setCarNum(carNum);
            alarm.setMemberId(tbUserInfo.getBjCode());
            
            int userId=tbCarlock.getUserID();
            String tel= userCacheInter.userId2tel(String.valueOf(userId));
            alarm.setMobile(tel);
            //将推送消息　改为发送短信
//          devicePushInterfaceService.outCheckAlarm(alarm);
            if(!StringUtils.isBlank(tel)){
                smsInterfaceServiec.lockcar_checkout_alarm(alarm);
            }
            
        }

    }
    
    /**
     * @Description (TODO锁车报警)
     * @param supplyId 厂商编号
     * @param parkCode  车场编号
     * @param ourRoom 出场岗停
     * @param carNum 出场车牌
     * @param alarmTime 报警时间
     */
    @Transactional(rollbackFor = Throwable.class)
    public void alarmCockCarArk( String supplyId,String parkCode,String ourRoom,String carNum, Date alarmTime ) {
        
        TbParkspace tbParkspace = configDataCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyId));
        int parkId = tbParkspace.getId();
        TbUserInfo tbUserInfo = tbCarlockMapper.queryLockUser(parkId, carNum);
        TblockcarAlarm tblockcarAlarm = new TblockcarAlarm();
        if(tbUserInfo==null){
            _log.error("没有找到锁车用户....不进行处理");
            return;
        }
        tblockcarAlarm.setCarNum(carNum);
        tblockcarAlarm.setParkID(parkId);
        tblockcarAlarm.setParkName(tbParkspace.getName());
        tblockcarAlarm.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
        tblockcarAlarm.setUserID(tbUserInfo.getId());
        int num = tblockcarAlarmMapper.save(tblockcarAlarm);
        TbCarlock tbCarlock=tbCarlockMapper.queryByParkIdCarNum(parkId, carNum);
        if (num != 0&&tbCarlock!=null) {
            OutCheckAlarm alarm = new OutCheckAlarm();
            alarm.setCarNum(carNum);
            alarm.setMemberId(tbUserInfo.getBjCode());
            
            int userId=tbCarlock.getUserID();
            String tel= userCacheInter.userId2tel(String.valueOf(userId));
            alarm.setMobile(tel);
            if(!StringUtils.isBlank(tel)){
                smsInterfaceServiec.lockcar_checkout_alarm(alarm);
            }
            
        }

    }

    
    @Transactional(rollbackFor = Throwable.class)
    public void lockTimeOut(TbCarlock carlock) throws AdapterProxyCreateFail, AdapterException {
        String id = carlock.getId();
        TbCarlock tbCarlock = tbCarlockMapper.queryById(id);
        tbCarlock.setStatus(CarLockStatusEnum.OVERTIME_STATUS.getStatus());
        int num = tbCarlockMapper.lockTimeOut(tbCarlock);

        if (num == 0) {
            _log.info("id:" + id + " 没有此状态数据");
//          return;
        }

        /****** 向车场下发解锁车单 ***************/
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByParkId(tbCarlock.getParkID());
        int supplierId = tbParkspace.getSupplierId();
        String parkCode = tbParkspace.getParkCode();
        String carNum = tbCarlock.getCarNum();
        LockCarAdapter adapterProxy =adapterParkProxyFactory.getLockCarAdapter(String.valueOf(supplierId));
        AdLockCarOrder apLockCarOrder = new AdLockCarOrder();
        apLockCarOrder.setCarNum(carNum);
        apLockCarOrder.setOrderId(String.valueOf(id));
        apLockCarOrder.setParkCode(parkCode);
        apLockCarOrder.setStartTime(tbCarlock.getStartTime());
        apLockCarOrder.setEndTime(tbCarlock.getEndTime());
        adapterProxy.unLockCar(apLockCarOrder);
        /****** 向车场下发解锁车单 ***************/

    }
}
